<template>
  <div>
    <el-dialog
      top="80px"
      width="70%"
      append-to-body
      :visible.sync="visible"
      :before-close="hanldClose"
      :close-on-click-modal="false"
    >
      <h5 align="center">走访登记表</h5>
      <div class="card">
        <div class="card-body">
          <div class="dl-wrap">
            <div class="dl-dd">
              <el-input
                type="textarea"
                v-model="PostData.visit_des"
                :autosize="{ minRows: 4, maxRows: 100 }"
                placeholder="请填走访情况"
              ></el-input>
            </div>
          </div>

          <div class="dl-wrap">
            <div class="dl-dt">走访时间：</div>
            <div class="dl-dd">
              <el-date-picker
                v-model="PostData.visit_date"
                @change="setTime"
                placeholder="走访时间"
                type="date"
                value-format="yyyy-MM-dd"
                :clearable="true"
              ></el-date-picker>
            </div>
          </div>
        </div>
      </div>

      <div class="visit-fuj" style="text-align: center">
        <el-button @click="hanldClose">取消</el-button>
        <el-button
          type="primary"
          v-loading="saveLoading"
          @click="submitdes('credit', 'credit_visit_record')"
        >
          保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// const visdate = new Date().toLocaleDateString()
export default {
  name: 'Visithistory',
  props: {
    visible: Boolean,
    relatedData: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      related: {},
      PostData: {},
      saveLoading: false,
    }
  },
  watch: {
    relatedData: {
      handler: function (val) {
        if (val.customer_related_id) {
          this.related = { ...val }
        }
      },
      deep: true,
      immediate: true,
    },
  },
  created() {
    this.initRelated()
  },
  computed: {
    user() {
      return this.$store.getters.user
    },
  },
  methods: {
    initRelated() {
      if (this.related.customer_related_id) {
        return
      }
      this.related = JSON.parse(sessionStorage.getItem('customer_related_view'))
    },
    hanldClose() {
      this.$emit('update:visible', false)
    },
    async submitdes(service, tableName) {
      let data = {}
      data.customer_id = this.related.customer_id
      data.customer_name = this.related.customer_name
      data.customer_related_id = this.related.customer_related_id
      data.visit_person = this.user.realName
      data.visit_date = this.PostData.visit_date
      data.visit_des = this.PostData.visit_des
      data.visit_type = '1'
      if (!data.visit_date) {
        this.$message.error('走访时间必填！')
        return
      }
      this.saveLoading = true
      const [res, err] = await this.$utils.promise(this.$api.addData(service, tableName, data))
      this.saveLoading = false
      if (res) {
        this.$message.success('数据采集成功！')
        this.$emit('success', true)
        this.hanldClose()
      } else {
        this.$message.error(`数据采集失败，请重试！（${err.status}）`)
      }
    },
  },
}
</script>

<style scoped></style>
